import Mock from 'mockjs'

Mock.setup({
    timeout: '200'
})
// ProcessManage 的 mock数据
let {tableData} = Mock.mock({
    "tableData|40": [
        // 姓名                 // 备注           // 创建人                                 // 创建时间              // 修改人              // 表名             // 流程名称                             // 
        {
            'id|+1': 1,
            name: '@cname(2,3)',
            remark: '@cword(3)',
            'creator|1': ["超级管理员","普通管理员","非管理员"],
            creationTime: '@now()',
            modifier: '@cname(2,4)',
            tableName: '@protocol()',
            'process|1': ["测试","审核","完成"],
            'feature|1': ["产品管理","产品审核"],
            'start|1': ["是","否"],
            state:false
        }
    ],
})

// ProcessManage 全部数据
Mock.mock('/text/listAll', 'get', () => {
    return tableData
})

// ProcessManage请求id
Mock.mock(/^\/text\/listAll\/GetId/, 'get', (option) => {
    let search = option.url.split('?')[1]
    let id = search.split('=')[1]
    return tableData.find(s => s.id == id)
})
// ProcessManage 分页+模糊查询
Mock.mock(/^\/text\/listAll\/GetPageAll/, 'get', (option) => {
    let search = option.url.split('?')[1]
    let params = {}
    search.split('&').forEach(r => {
        let arr = r.split('=')
        params[arr[0]] = arr[1]
    })
    // console.log(params);
    let { pageIndex, pageSize, process } = params
    let string = decodeURIComponent(process)
    // console.log(string);
    if (string == '') {
        return {
            data: tableData.slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
            count: tableData.length
        }
    } else {
        return {
            data: wzhdemohuchaxun(string, tableData).slice((pageIndex - 1) * pageSize, pageIndex * pageSize),
            count: tableData.length
        }
    }
})
// ProcessManage 修改接口
Mock.mock('/text/listAll/UpDate', 'post', (option) => {
    console.log(option);
    let obj = JSON.parse(option.body)
        let sub = tableData.find(s => s.id == obj.id)
        sub.id = obj.id
        sub.newName = obj.newName
        sub.remark = obj.remark
        sub.creationTime = obj.creationTime
        sub.feature = obj.feature
        sub.state = obj.state
        sub.start = obj.start
        sub.process = obj.process
        sub.tableName = obj.tableName
        sub.modifier = obj.modifier
        sub.creator = obj.creator
        return sub
})

// ProcessManage 删除接口
Mock.mock('/text/listAll/del', 'post', (option) => {
    let obj = JSON.parse(option.body)
    obj.forEach(r=>{
        let sub = tableData.findIndex(s => s.id == r.id)
        tableData.splice(sub, 1)
    })
    return true
})
// ProcessManage 添加接口
Mock.mock('/text/listAll/add', 'post', (option) => {
    // console.log(option);
    let obj = JSON.parse(option.body)
    console.log(obj);
    let sub = {
        id: tableData.length + 1,
        newName:obj.name,
        remark:obj.remark,
        creator:obj.creator,
        creationTime:obj.creationTime,
        modifier:obj.modifier,
        tableName:obj.tableName,
        process:obj.process,
        feature:obj.feature,
        start:obj.start,
        state:obj.state
    }
    tableData.push(sub)
    return true
})


function wzhdemohuchaxun(name, object) {
    let obj = []
    let a = []
    object.forEach(r => {
        a.push(r.process)
    })
    let arr = [];
    for (var i = 0; i < a.length; i++) {
        if (a[i].indexOf(name) >= 0) {
            arr.push(a[i]);
        }
    }
    arr.forEach(r => {
        obj.push(object.find(s => s.process == r))
    })
    return obj

}

export default Mock